package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.KeywordParam;
import com.cskaoyan.bean.bo.UpdateKeywordBO;
import com.cskaoyan.bean.pojo.MallKeyword;
import com.cskaoyan.bean.pojo.MallKeywordExample;
import com.cskaoyan.bean.bo.KeywordBO;
import com.cskaoyan.bean.vo.KeywordsInfoVO;
import com.cskaoyan.mapper.MallKeywordMapper;

import com.cskaoyan.service.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: Chunlai
 * @description:
 * @time: 2021/7/9 11:45
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MallKeywordMapper mallKeywordMapper ;

    @Override
    public BaseData<MallKeyword> querykeywordinfos(KeywordParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallKeywordExample mallKeywordExample = new MallKeywordExample();
        MallKeywordExample.Criteria criteria = mallKeywordExample.createCriteria();
        if (param.getKeyword() != null){
            criteria.andKeywordLike("%"+param.getKeyword()+"%");
        }
        if (param.getUrl() != null){
            criteria.andUrlLike("%"+param.getUrl()+"%");
        }
        criteria.andDeletedEqualTo(false);

        mallKeywordExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallKeyword> mallKeywords = mallKeywordMapper.selectByExample(mallKeywordExample);
        PageInfo<MallKeyword> pageInfo = new PageInfo<>(mallKeywords);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(mallKeywords, total);
    }


    public KeywordsInfoVO insertkeywordinfos(KeywordBO param) {
        MallKeyword mallKeyword = new MallKeyword(null,param.getKeyword(),param.getUrl(),param.getIsDefault(),param.getIsHot(),null,new Date(),new Date(),null);
        int insert = mallKeywordMapper.insert(mallKeyword);
        Integer id = mallKeyword.getId();
        System.out.println("id " + id);
        MallKeyword mallKeyword1 = mallKeywordMapper.selectByPrimaryKey(id);
        KeywordsInfoVO keywordsInfoVO = new KeywordsInfoVO();
//        KeywordsInfoVO keywordsInfoVO = new KeywordsInfoVO(mallKeyword1.getId(),mallKeyword1.getKeyword(),mallKeyword1.getUrl(),mallKeyword1.getIsHot(),mallKeyword1.getIsDefault(),mallKeyword1.getSortOrder(),mallKeyword1.getAddTime(),mallKeyword1.getUpdateTime(),mallKeyword1.getDeleted());
        return keywordsInfoVO;
    }

    @Override
    public KeywordsInfoVO insertkeywordinfos2(KeywordBO param) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format_time = simpleDateFormat.format(date);

        param.setAddTime(format_time);
        param.setUpdateTime(format_time);

        int index = mallKeywordMapper.insertByparam(param);

        Integer id = param.getId();
        MallKeyword mallKeyword = mallKeywordMapper.selectByPrimaryKey(id);

        KeywordsInfoVO keywordsInfoVO = new KeywordsInfoVO(mallKeyword.getId(),mallKeyword.getKeyword(),mallKeyword.getUrl(),mallKeyword.getIsHot(),mallKeyword.getIsDefault(),String.valueOf(mallKeyword.getAddTime()),String.valueOf(mallKeyword.getUpdateTime()));
        System.out.println(keywordsInfoVO);
        return keywordsInfoVO;
    }


    public MallKeyword updatekeyword1(UpdateKeywordBO param) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String addTime = param.getAddTime();
//
//        String updateTime = param.getUpdateTime();
//
//        Date parse1 = simpleDateFormat.parse(addTime);

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = simpleDateFormat1.format(date);
        Date parse = simpleDateFormat1.parse(format);

        MallKeyword mallKeyword = new MallKeyword(param.getId(),param.getKeyword(),param.getUrl(),param.getIsHot(),param.getIsDefault(),null,null,parse,null);
        mallKeywordMapper.updateByPrimaryKeySelective(mallKeyword);
        MallKeyword mallKeyword1 = mallKeywordMapper.selectByPrimaryKey(param.getId());
        return mallKeyword1;
    }

    @SneakyThrows
    @Override
    public MallKeyword updatekeyword(UpdateKeywordBO param) {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = simpleDateFormat1.format(date);
        Date parse = simpleDateFormat1.parse(format);

        MallKeyword mallKeyword = new MallKeyword(param.getId(),param.getKeyword(),param.getUrl(),param.getIsHot(),param.getIsDefault(),null,null,parse,null);
        mallKeywordMapper.updateByPrimaryKeySelective(mallKeyword);
        MallKeyword mallKeyword1 = mallKeywordMapper.selectByPrimaryKey(param.getId());
        return mallKeyword1;

    }
    @Override
    public void delkeyword(UpdateKeywordBO param) {
        int id = param.getId();
        mallKeywordMapper.updatedeletedByPrimaryKey(id);
    }
}